//
//  EXT+UIBarButtonItem.swift
//  Swift---新浪微博练习代码
//
//  Created by Mr.Yan on 2017/6/29.
//  Copyright © 2017年 Mr.Yan. All rights reserved.
//

import UIKit

extension UIBarButtonItem {

    convenience init(imageName:String?, title:String?, tager:Any?, action: Selector){
    
        //实例化一个按钮
        let button = UIButton()
        //监听事件
        button.addTarget(tager, action: action, for: UIControlEvents.touchUpInside)
        if let image = imageName{
        //图片
            button.setImage(UIImage(named:image), for: UIControlState.normal)
            button.setImage(UIImage(named:"\(image)_highlighted"), for: UIControlState.highlighted)
        
        }
        if let title = title {
        
            button.setTitle(title, for: UIControlState.normal)
            button.titleLabel?.font = UIFont.systemFont(ofSize: 14)
            button.setTitleColor(UIColor.darkGray, for: UIControlState.normal)
            button.setTitleColor(UIColor.orange, for: UIControlState.highlighted)
        
        }
    //按钮自适应
        button.sizeToFit()
        //调用指定函数完成实例化
        self.init(customView: button)
    }

}


